package com.tuling.mybatis.dao;

import com.tuling.mybatis.dao.User;
import com.tuling.mybatis.dao.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

/**
 * @Author: lijunwei
 * @Time: 2020/9/15 9:16
 */
public class TestMybatis {
    public static void main(String[] args) throws Exception {
        // 设置资源路径，指定配置文件的位置：
        String resource = "mybatis-config-gp.xml";
        // 加载配置文件，获取输入流
        InputStream inputStream = Resources.getResourceAsStream(resource);
        // 通过SqlSessionFactoryBuilder构建SqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        // 获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        // 执行指定的statement，完成查询
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.selectByid(1);
        System.out.println("---------------"+user);

        sqlSession.close();


        SqlSession sqlSession1 = sqlSessionFactory.openSession();
        UserMapper mapper1 = sqlSession1.getMapper(UserMapper.class);
        User user1 = mapper1.selectByid(1);
        System.out.println("---------------"+user1);

        System.out.println(user==user1);


    }
}
